package com.shanghaionstar.servicepackage.adapter;

import android.content.Context;
import android.view.View;
import android.widget.Button;

import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.base.CommonAdapter;
import com.onstar.cn.framework.base.CommonViewHolder;
import com.onstar.cn.more.entity.PackageInfo;
import com.shanghaionstar.R;

import java.util.List;

public class OnstarChoosePkgAdapter extends CommonAdapter<PackageInfo> {

    private static final String TAG = "PayMeOrderAdapter";
    private List<PackageInfo> groupList;
    private Context context;
    private int position = -1;
    private int selectPosition = -1;
    private PackageInfo selectPackInfo;
    //	private String year = "1";
    /*定义一个类CallBack的引用mCallBack，可根据需要生成getter/setter mCallback引用（指针）负责调用PayMeOrderActivity中的onDataClick(int p)函数*/
    private EbCallBackListener mCallback;

    /*定义一个类也叫抽象类特殊的抽象类也叫接口，负责调用PayMeOrderActivity中的onDataClick(int p)函数*/
    public interface EbCallBackListener {
        void onEBDataCallBack(int p);
    }

    public OnstarChoosePkgAdapter(List<PackageInfo> groupList, Context context, EbCallBackListener cb) {
        super(groupList, context);
        this.groupList = groupList;
        this.context = context;
        this.mCallback = cb;
    }

    @Override
    public int getCount() {
        if (groupList != null) {
            return groupList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (groupList != null && position < groupList.size()) {
            return groupList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public int getSelectPosition() {
        return selectPosition;
    }

    public void setSelectPosition(int selectPosition) {
        this.selectPosition = selectPosition;
    }

    public PackageInfo getSelectPackInfo() {
        return selectPackInfo;
    }

    public void setSelectPackInfo(PackageInfo selectPackInfo) {
        this.selectPackInfo = selectPackInfo;
    }

    public String degreeCode;

    public void setGroupList(List<PackageInfo> groupList) {
        this.groupList = groupList;
    }

    public void setDegreeCode(String degreeCode) {
        this.degreeCode = degreeCode;
    }

    public String getDegreeCode() {
        return degreeCode;
    }


    private class ViewHolder extends CommonViewHolder {
        @FindViewById
        private Button tv_order_name;
    }

    @Override
    public void getView(final int groupPosition, CommonViewHolder viewHolder) {

        ViewHolder holder = (ViewHolder) viewHolder;
        if (groupList != null && groupList.size() > 0) {
            PackageInfo packageInfo = groupList.get(groupPosition);
            if (packageInfo != null) {
                if (degreeCode.equals(packageInfo.getDegreeCode())) {
                    holder.tv_order_name.setText(packageInfo.getPackageName());
                    // 选中的套餐包信息
                    if (selectPosition == groupPosition) {
                        if ("1".equals(packageInfo.getIsEBPackage())) {
                            holder.tv_order_name.setBackgroundResource(R.drawable.eb_checked);
                        } else {
                            holder.tv_order_name.setBackgroundResource(R.drawable.sel_bg);
                        }
                        mCallback.onEBDataCallBack(groupPosition); //yongqiang tao
                    } else {
                        if ("1".equals(packageInfo.getIsEBPackage())) {
                            holder.tv_order_name.setBackgroundResource(R.drawable.eb_uncheck);
                        } else {
                            holder.tv_order_name.setBackgroundResource(R.drawable.nor_sel_bg);
                        }
                    }
                    holder.tv_order_name.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            selectPackInfo = groupList.get(groupPosition);
                            selectPosition = groupPosition;
                            notifyDataSetChanged();
                        }
                    }); //抢夺焦点 yongqiang tao
                }
            }
        }
    }

    @Override
    public int layoutId() {
        return R.layout.onstar_payme_order_list_group_item;
    }

    @Override
    public CommonViewHolder getViewHolder() {
        // TODO Auto-generated method stub
        return new ViewHolder();
    }

}
